
<!DOCTYPE HTML>
<html lang="" >
    <head>
        <meta charset="UTF-8">
        <meta content="text/html; charset=utf-8" http-equiv="Content-Type">
        <title>HttpClient几个超时 · GitBook</title>
        <meta http-equiv="X-UA-Compatible" content="IE=edge" />
        <meta name="description" content="">
        <meta name="generator" content="GitBook 3.2.3">
        
        
        
    
    <link rel="stylesheet" href="../gitbook/style.css">

    
            
                
                <link rel="stylesheet" href="../gitbook/gitbook-plugin-toggle-chapters/toggle.css">
                
            
                
                <link rel="stylesheet" href="../gitbook/gitbook-plugin-highlight/website.css">
                
            
                
                <link rel="stylesheet" href="../gitbook/gitbook-plugin-search/search.css">
                
            
                
                <link rel="stylesheet" href="../gitbook/gitbook-plugin-fontsettings/website.css">
                
            
        

    

    
        
    
        
    
        
    
        
    
        
    
        
    

        
    
    
    <meta name="HandheldFriendly" content="true"/>
    <meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=no">
    <meta name="apple-mobile-web-app-capable" content="yes">
    <meta name="apple-mobile-web-app-status-bar-style" content="black">
    <link rel="apple-touch-icon-precomposed" sizes="152x152" href="../gitbook/images/apple-touch-icon-precomposed-152.png">
    <link rel="shortcut icon" href="../gitbook/images/favicon.ico" type="image/x-icon">

    
    <link rel="next" href="httpclient-pool.html" />
    
    
    <link rel="prev" href="../" />
    

    </head>
    <body>
        
<div class="book">
    <div class="book-summary">
        
            
<div id="book-search-input" role="search">
    <input type="text" placeholder="Type to search" />
</div>

            
                <nav role="navigation">
                


<ul class="summary">
    
    

    

    
        
        
    
        <li class="chapter " data-level="1.1" data-path="../">
            
                <a href="../">
            
                    
                    简介
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2" data-path="../">
            
                <a href="../">
            
                    
                    编辑器
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.2.1" data-path="../editortools/SublineText3-Quick-Key.html">
            
                <a href="../editortools/SublineText3-Quick-Key.html">
            
                    
                    SublineText3快捷键
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2.2" data-path="../editortools/SublineText3.html">
            
                <a href="../editortools/SublineText3.html">
            
                    
                    SublineText3问题
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2.3" data-path="../editortools/iTerm2-guide.html">
            
                <a href="../editortools/iTerm2-guide.html">
            
                    
                    iTerm2指南
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.3" data-path="../">
            
                <a href="../">
            
                    
                    乱七八糟
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.3.1" data-path="../sundry/git-note.html">
            
                <a href="../sundry/git-note.html">
            
                    
                    Git 简单笔记
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.2" data-path="../sundry/gitbook-doc.html">
            
                <a href="../sundry/gitbook-doc.html">
            
                    
                    gitbook 教程
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.3" data-path="../sundry/gitbook-to-github.html">
            
                <a href="../sundry/gitbook-to-github.html">
            
                    
                    gitbook 集成到Github
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.4" data-path="../sundry/git-integrate.html">
            
                <a href="../sundry/git-integrate.html">
            
                    
                    Git提速
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.5" data-path="../sundry/markdown-doc.html">
            
                <a href="../sundry/markdown-doc.html">
            
                    
                    markdown 语法
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.6" data-path="../sundry/maven-nexus.html">
            
                <a href="../sundry/maven-nexus.html">
            
                    
                    Maven 私服搭建
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.7" data-path="../sundry/idea-config.html">
            
                <a href="../sundry/idea-config.html">
            
                    
                    IDEA 配置
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.8" data-path="../sundry/idea-quick-key.html">
            
                <a href="../sundry/idea-quick-key.html">
            
                    
                    IDEA 快捷键
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.9" data-path="../sundry/iterm2.html">
            
                <a href="../sundry/iterm2.html">
            
                    
                    iTerm2
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.10" >
            
                <a target="_blank" href="http://fontawesome.dashgame.com/">
            
                    
                    Font Awesome字体
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.4" data-path="../">
            
                <a href="../">
            
                    
                    mariadb
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.4.1" data-path="../mariadb/mariadb-install.html">
            
                <a href="../mariadb/mariadb-install.html">
            
                    
                    mariadb 安装
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.4.2" data-path="../mariadb/mysql-install.html">
            
                <a href="../mariadb/mysql-install.html">
            
                    
                    mysql 安装
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.4.3" data-path="../mariadb/mysql-base.html">
            
                <a href="../mariadb/mysql-base.html">
            
                    
                    mysql 基础知识
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.5" data-path="../">
            
                <a href="../">
            
                    
                    MongoDB
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.5.1" data-path="../mongodb/mongodb-install.html">
            
                <a href="../mongodb/mongodb-install.html">
            
                    
                    MongoDB 安装
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.6" data-path="../">
            
                <a href="../">
            
                    
                    nginx
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.6.1" data-path="../nginx/nginx-install.html">
            
                <a href="../nginx/nginx-install.html">
            
                    
                    Nginx 安装
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.6.2" data-path="../nginx/nginx-commandline.html">
            
                <a href="../nginx/nginx-commandline.html">
            
                    
                    Ngixn 命令
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.6.3" data-path="../nginx/nginx-virtualhost.html">
            
                <a href="../nginx/nginx-virtualhost.html">
            
                    
                    Nginx 虚拟主机
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.7" data-path="../">
            
                <a href="../">
            
                    
                    HttpClient
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter active" data-level="1.7.1" data-path="httpclient-timeout.html">
            
                <a href="httpclient-timeout.html">
            
                    
                    HttpClient几个超时
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.7.2" data-path="httpclient-pool.html">
            
                <a href="httpclient-pool.html">
            
                    
                    HttpClient 连接池
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.8" data-path="../">
            
                <a href="../">
            
                    
                    Java
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.8.1" data-path="../java/001-tomcat-loader-cpu-high.html">
            
                <a href="../java/001-tomcat-loader-cpu-high.html">
            
                    
                    01-Tomcat应用负载过高—CPU过高
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.9" data-path="../">
            
                <a href="../">
            
                    
                    Spring与SpringBoot
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.9.1" data-path="../SpringBoot/001-SpringBoot-form-jar-to-war.html">
            
                <a href="../SpringBoot/001-SpringBoot-form-jar-to-war.html">
            
                    
                    01-SpringBoot-Jar包转war包
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.10" data-path="../">
            
                <a href="../">
            
                    
                    RPC
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.10.1" data-path="../rpc/thrift/thrift-01-intro.html">
            
                <a href="../rpc/thrift/thrift-01-intro.html">
            
                    
                    01-Thrift简介
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.10.2" data-path="../rpc/thrift/thrift-02-install.html">
            
                <a href="../rpc/thrift/thrift-02-install.html">
            
                    
                    02-Thrift安装
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.10.3" data-path="../rpc/thrift/thrift-03-types.html">
            
                <a href="../rpc/thrift/thrift-03-types.html">
            
                    
                    03-Thrift数据类型
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.10.4" data-path="../rpc/thrift/thrift-04-generated-code.html">
            
                <a href="../rpc/thrift/thrift-04-generated-code.html">
            
                    
                    04-Thrift代码生成
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.10.5" data-path="../rpc/thrift/thrift-05-architecture.html">
            
                <a href="../rpc/thrift/thrift-05-architecture.html">
            
                    
                    05-Thrift架构设计
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.11" data-path="../">
            
                <a href="../">
            
                    
                    线程世界
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.11.1" data-path="../thread/01-traditional-thread.html">
            
                <a href="../thread/01-traditional-thread.html">
            
                    
                    01-传统线程技术
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.11.2" data-path="../thread/02-traditional-mutex.html">
            
                <a href="../thread/02-traditional-mutex.html">
            
                    
                    02-线程之间的互斥
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.11.3" data-path="../thread/03-traditional-communication.html">
            
                <a href="../thread/03-traditional-communication.html">
            
                    
                    03-线程之间的通信
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.11.4" data-path="../thread/04-traditional-thread-state.html">
            
                <a href="../thread/04-traditional-thread-state.html">
            
                    
                    04-线程状态
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.11.5" data-path="../thread/05-traditional-thread-join.html">
            
                <a href="../thread/05-traditional-thread-join.html">
            
                    
                    05-线程join方法
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.12" data-path="../">
            
                <a href="../">
            
                    
                    Linux
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.12.1" data-path="../linux/001-linux-command.html">
            
                <a href="../linux/001-linux-command.html">
            
                    
                    01-Linux常用命令总结
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.12.2" data-path="../linux/002-linux-firewall.html">
            
                <a href="../linux/002-linux-firewall.html">
            
                    
                    02-Linux防火墙
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.12.3" data-path="../linux/003-linux-tmux.html">
            
                <a href="../linux/003-linux-tmux.html">
            
                    
                    03-Linux-Tmux
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.13" data-path="../">
            
                <a href="../">
            
                    
                    数据结构与算法
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.13.1" data-path="../datastructure/01-intro.html">
            
                <a href="../datastructure/01-intro.html">
            
                    
                    01-简介
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.14" data-path="../">
            
                <a href="../">
            
                    
                    前端
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.14.1" data-path="../front/01-yarn.html">
            
                <a href="../front/01-yarn.html">
            
                    
                    01-yarn
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.14.2" data-path="../front/02-jsx.html">
            
                <a href="../front/02-jsx.html">
            
                    
                    02-jsx语法
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.14.3" data-path="../front/03-react-component.html">
            
                <a href="../front/03-react-component.html">
            
                    
                    03-React组件
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.14.4" data-path="../front/04-ES6.html">
            
                <a href="../front/04-ES6.html">
            
                    
                    04-ES6
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.14.5" data-path="../front/05-webpack.html">
            
                <a href="../front/05-webpack.html">
            
                    
                    05-webpack
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    

    

    <li class="divider"></li>

    <li>
        <a href="https://www.gitbook.com" target="blank" class="gitbook-link">
            Published with GitBook
        </a>
    </li>
</ul>


                </nav>
            
        
    </div>

    <div class="book-body">
        
            <div class="body-inner">
                
                    

<div class="book-header" role="navigation">
    

    <!-- Title -->
    <h1>
        <i class="fa fa-circle-o-notch fa-spin"></i>
        <a href=".." >HttpClient几个超时</a>
    </h1>
</div>




                    <div class="page-wrapper" tabindex="-1" role="main">
                        <div class="page-inner">
                            
<div id="book-search-results">
    <div class="search-noresults">
    
                                <section class="normal markdown-section">
                                
                                <h1 id="httpclient-&#x8D85;&#x65F6;&#x8BF4;&#x660E;">HttpClient &#x8D85;&#x65F6;&#x8BF4;&#x660E;</h1>
<p>Apache HttpClient &#x662F;&#x5F00;&#x53D1;&#x4E2D;&#x6700;&#x5E38;&#x7528;&#x7684;&#x5DE5;&#x5177;&#xFF0C;&#x4F7F;&#x7528;&#x65B9;&#x4FBF;&#xFF0C;&#x4F46;&#x662F;&#x5BF9;&#x5176;&#x7EC6;&#x8282;&#x4E86;&#x89E3;&#x7684;&#x5374;&#x662F;&#x4E0D;&#x591A;&#x3002;&#x672C;&#x6587;&#x662F;&#x57FA;&#x4E8E; HttpClient 4.5.8 &#x7248;&#x672C;&#x7684;&#x3002;</p>
<p>&#x6211;&#x4EEC;&#x4F7F;&#x7528; HttpClient &#x53EF;&#x80FD;&#x7ECF;&#x5E38;&#x4F1A;&#x770B;&#x5230;&#x5982;&#x4E0B;&#x4EE3;&#x7801;&#xFF0C;&#x4F46;&#x662F;&#x80FD;&#x8BF4;&#x6E05;&#x695A;&#x6BCF;&#x4E2A;&#x8D85;&#x65F6;&#x7684;&#x542B;&#x4E49;&#x5417;&#xFF1F;</p>
<pre><code class="lang-java">   RequestConfig requestConfig = RequestConfig.custom()
                .setSocketTimeout(<span class="hljs-number">1000</span>)
                .setConnectionRequestTimeout(<span class="hljs-number">5000</span>)
                .setConnectTimeout(<span class="hljs-number">5</span>).build();
</code></pre>
<h3 id="1-connecttimeout-&#x8FDE;&#x63A5;&#x8D85;&#x65F6;">1. ConnectTimeout &#x8FDE;&#x63A5;&#x8D85;&#x65F6;</h3>
<ul>
<li>setConnectTimeout &#x662F;&#x6307;&#x4E0E;&#x76EE;&#x6807;&#x4E3B;&#x673A;&#x5EFA;&#x7ACB;&#x8FDE;&#x63A5;&#x8D85;&#x65F6;
&#x8BF7;&#x770B;&#x5982;&#x4E0B;&#x793A;&#x4F8B;&#xFF1A;</li>
</ul>
<pre><code class="lang-java">    <span class="hljs-meta">@Test</span>
    <span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title">testConnectionTimeout</span><span class="hljs-params">()</span></span>{
        CloseableHttpClient client = HttpClients.createDefault();
        HttpGet httpGet = <span class="hljs-keyword">new</span> HttpGet(<span class="hljs-string">&quot;http://www.baidu.com&quot;</span>);
        RequestConfig requestConfig = RequestConfig.custom()
                    .setConnectTimeout(<span class="hljs-number">5</span>).build();
        httpGet.setConfig(requestConfig);
        <span class="hljs-keyword">try</span> {
            CloseableHttpResponse response = client.execute(httpGet);
            System.out.println(EntityUtils.toString(response.getEntity()));
        } <span class="hljs-keyword">catch</span> (Exception e){
            e.printStackTrace();
        }
    }
</code></pre>
<p>&#x8BBF;&#x95EE; <code>http://www.baidu.com</code> &#x5E76;&#x8BBE;&#x7F6E;&#x8D85;&#x65F6;&#x65F6;&#x95F4;&#x4E3A; 5 &#x6BEB;&#x79D2;&#x6216;&#x8005;&#x66F4;&#x5C0F;&#x7684;&#x503C;(&#x4E3A;&#x4E86;&#x663E;&#x793A;&#x6548;&#x679C;)&#xFF0C;&#x8FD0;&#x884C;&#x6B64;&#x4EE3;&#x7801;&#xFF0C;&#x4F1A;&#x51FA;&#x73B0;&#x5982;&#x4E0B;&#x5F02;&#x5E38;&#xFF1A;</p>
<pre><code class="lang-java">org.apache.http.conn.ConnectTimeoutException: Connect to www.baidu.com:<span class="hljs-number">80</span> [www.baidu.com/<span class="hljs-number">220.181</span>.38.150, www.baidu.com/<span class="hljs-number">220.181</span>.38.149] failed: connect timed out
</code></pre>
<p>&#x4E5F;&#x5C31;&#x662F;&#x8BF4;&#x5728; 5 &#x6BEB;&#x79D2;&#x7684;&#x65F6;&#x95F4;&#x5185;&#x4E0D;&#x80FD;&#x4E0E; <code>http://www.baidu.com</code> &#x5EFA;&#x7ACB;&#x8FDE;&#x63A5;&#xFF0C;&#x6240;&#x4EE5;&#x62A5;&#x4E86;&#x8FDE;&#x63A5;&#x8D85;&#x65F6;&#x7684;&#x5F02;&#x5E38;&#x3002;</p>
<h3 id="2-sockettimeout-&#x7B49;&#x5F85;&#x6570;&#x636E;&#x7684;&#x8D85;&#x65F6;&#x65F6;&#x95F4;">2. SocketTimeout &#x7B49;&#x5F85;&#x6570;&#x636E;&#x7684;&#x8D85;&#x65F6;&#x65F6;&#x95F4;</h3>
<ul>
<li>setSocketTimeout &#x7B49;&#x5F85;&#x6570;&#x636E;&#x7684;&#x8D85;&#x65F6;&#x65F6;&#x95F4;&#xFF0C;&#x4E0E;&#x76EE;&#x6807;&#x4E3B;&#x673A;&#x5EFA;&#x7ACB;&#x8FDE;&#x63A5;&#x540E;&#xFF0C;&#x4E24;&#x4E2A;&#x6570;&#x636E;&#x5305;&#x4E4B;&#x95F4;&#x4E0D;&#x6D3B;&#x52A8;&#x7684;&#x6700;&#x957F;&#x65F6;&#x95F4;</li>
</ul>
<p>&#x4E3A;&#x4E86;&#x8BF4;&#x660E;&#x8D85;&#x65F6;&#x6548;&#x679C;&#xFF0C;&#x5206;&#x4E24;&#x79CD;&#x60C5;&#x51B5;&#xFF1A;<br>2.1 &#x7B2C;&#x4E00;&#x79CD;&#x60C5;&#x51B5;
&#x670D;&#x52A1;&#x7AEF;&#x4EE3;&#x7801;</p>
<pre><code class="lang-java">    <span class="hljs-meta">@ResponseBody</span>
    <span class="hljs-meta">@RequestMapping</span>(value = <span class="hljs-string">&quot;testSocketTimeout&quot;</span>, method = RequestMethod.GET)
    <span class="hljs-function"><span class="hljs-keyword">public</span> String <span class="hljs-title">testSocketTimeout</span><span class="hljs-params">()</span> <span class="hljs-keyword">throws</span> InterruptedException </span>{
        log.info(<span class="hljs-string">&quot;receive request ===============&quot;</span>);
        <span class="hljs-comment">// &#x4E3A;&#x4E86;&#x6F14;&#x793A;&#x4E1A;&#x52A1;&#x6267;&#x884C;&#x7684;&#x8017;&#x65F6;&#x65F6;&#x95F4; 3 &#x79D2;</span>
        Thread.sleep(<span class="hljs-number">3000</span>);
        <span class="hljs-keyword">return</span> <span class="hljs-string">&quot;testSocketTimeout&quot;</span>;
    }
</code></pre>
<p>&#x5BA2;&#x6237;&#x7AEF;&#x4EE3;&#x7801;&#xFF1A;</p>
<pre><code class="lang-java">    <span class="hljs-meta">@Test</span>
    <span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title">testSocketTimeout</span><span class="hljs-params">()</span></span>{
        CloseableHttpClient client = HttpClients.createDefault();
        HttpGet httpGet = <span class="hljs-keyword">new</span> HttpGet(<span class="hljs-string">&quot;http://192.168.1.100:8080/http/testSocketTimeout&quot;</span>);
        RequestConfig requestConfig = RequestConfig.custom().setSocketTimeout(<span class="hljs-number">3500</span>).build();
        httpGet.setConfig(requestConfig);
        <span class="hljs-keyword">try</span> {
            CloseableHttpResponse response = client.execute(httpGet);
            System.out.println(EntityUtils.toString(response.getEntity()));
        } <span class="hljs-keyword">catch</span> (Exception e){
            e.printStackTrace();
        }
    }
</code></pre>
<p>&#x5982;&#x679C; SocketTimeout &#x7684;&#x65F6;&#x95F4;&#x7406;&#x8BBA;&#x4E0A;&#x8BBE;&#x7F6E;&#x5927;&#x4E8E; 3000 &#x6BEB;&#x79D2;&#x5C31;&#x4E0D;&#x4F1A;&#x62A5;&#x9519;&#xFF0C;&#x5982;&#x679C;&#x662F;&#x5C0F;&#x4E8E; 3000 &#x6BEB;&#x79D2;&#x5C31;&#x4F1A;&#x629B;&#x51FA;&#x5F02;&#x5E38;&#xFF0C;&#x5F02;&#x5E38;&#x5982;&#x4E0B;&#xFF1A;</p>
<pre><code class="lang-java">java.net.SocketTimeoutException: Read timed out
</code></pre>
<p>&#x4E5F;&#x5C31;&#x662F;&#x8BF4;&#x5728; 3 &#x79D2;&#x7684;&#x65F6;&#x95F4;&#x5185;&#xFF0C;&#x5BA2;&#x6237;&#x7AEF;&#x6CA1;&#x80FD;&#x4ECE;&#x670D;&#x52A1;&#x7AEF;&#x83B7;&#x53D6;&#x4EFB;&#x4F55;&#x6570;&#x636E;&#x3002;</p>
<p>2.2 &#x7B2C;&#x4E8C;&#x79CD;&#x60C5;&#x51B5;</p>
<p>&#x670D;&#x52A1;&#x7AEF;&#x4EE3;&#x7801;&#xFF1A;</p>
<pre><code class="lang-java">    <span class="hljs-meta">@ResponseBody</span>
    <span class="hljs-meta">@RequestMapping</span>(value = <span class="hljs-string">&quot;testSocketTimeout2&quot;</span>, method = RequestMethod.GET)
    <span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title">testSocketTimeout2</span><span class="hljs-params">(HttpServletResponse response)</span> <span class="hljs-keyword">throws</span> Exception </span>{
        log.info(<span class="hljs-string">&quot;receive request ===============&quot;</span>);
        <span class="hljs-keyword">for</span> (<span class="hljs-keyword">int</span> i=<span class="hljs-number">0</span>;i&lt;<span class="hljs-number">10</span>;i++){
            response.getWriter().println(<span class="hljs-string">&quot;This is number : &quot;</span> + i);
            response.flushBuffer();
            <span class="hljs-comment">// &#x6BCF;&#x9694; 800 &#x6BEB;&#x79D2;&#x5411;&#x5BA2;&#x6237;&#x7AEF;&#x53D1;&#x9001;&#x6570;&#x636E;,&#x5171;&#x53D1;&#x9001; 10 &#x6B21;</span>
            Thread.sleep(<span class="hljs-number">800</span>);
        }
    }
</code></pre>
<p>&#x5BA2;&#x6237;&#x7AEF;&#x4EE3;&#x7801;</p>
<pre><code class="lang-java">    <span class="hljs-meta">@Test</span>
    <span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title">testSocketTimeout2</span><span class="hljs-params">()</span></span>{
        CloseableHttpClient client = HttpClients.createDefault();
        HttpGet httpGet = <span class="hljs-keyword">new</span> HttpGet(<span class="hljs-string">&quot;http://192.168.1.100:8080/http/testSocketTimeout2&quot;</span>);
        RequestConfig requestConfig = RequestConfig.custom().setSocketTimeout(<span class="hljs-number">1000</span>).build(); 
        httpGet.setConfig(requestConfig);
        <span class="hljs-keyword">try</span> {
            CloseableHttpResponse response = client.execute(httpGet);
            System.out.println(EntityUtils.toString(response.getEntity()));
        } <span class="hljs-keyword">catch</span> (Exception e){
            e.printStackTrace();
        }
    }
</code></pre>
<p>&#x5982;&#x679C; SocketTimeout &#x7684;&#x65F6;&#x95F4;&#x7406;&#x8BBA;&#x4E0A;&#x8BBE;&#x7F6E;&#x5927;&#x4E8E; 800 &#x6BEB;&#x79D2;&#x5C31;&#x4E0D;&#x4F1A;&#x62A5;&#x9519;&#xFF0C;&#x5982;&#x679C;&#x662F;&#x5C0F;&#x4E8E; 800 &#x6BEB;&#x79D2;&#x5C31;&#x4F1A;&#x629B;&#x51FA;&#x5F02;&#x5E38;&#xFF0C;&#x5F02;&#x5E38;&#x5982;&#x4E0B;&#xFF1A;</p>
<pre><code class="lang-java">java.net.SocketTimeoutException: Read timed out
</code></pre>
<p>&#x4E5F;&#x5C31;&#x662F;&#x8BF4;&#x5BA2;&#x6237;&#x7AEF;&#x5728; 800 &#x6BEB;&#x79D2;&#x5DE6;&#x53F3;&#x5C31;&#x4F1A;&#x6536;&#x5230;&#x670D;&#x52A1;&#x7AEF;&#x7684;&#x6570;&#x636E;&#x3002;&#x8BBE;&#x7F6E;&#x8D85;&#x65F6;&#x65F6;&#x95F4;&#x4E3A; 1000&#xFF0C;&#x6240;&#x4EE5;&#x4E0D;&#x4F1A;&#x8D85;&#x65F6;&#x3002;</p>
<h3 id="3-connectionrequesttimeout-&#x4ECE;&#x8FDE;&#x63A5;&#x6C60;&#x4E2D;&#x53D6;&#x5F97;&#x8FDE;&#x63A5;&#x7684;&#x7B49;&#x5F85;&#x65F6;&#x95F4;">3. ConnectionRequestTimeout &#x4ECE;&#x8FDE;&#x63A5;&#x6C60;&#x4E2D;&#x53D6;&#x5F97;&#x8FDE;&#x63A5;&#x7684;&#x7B49;&#x5F85;&#x65F6;&#x95F4;</h3>
<ul>
<li>setConnectionRequestTimeout  &#x4ECE;&#x8FDE;&#x63A5;&#x6C60;&#x4E2D;&#x53D6;&#x5F97;&#x8FDE;&#x63A5;&#x7684;&#x7B49;&#x5F85;&#x65F6;&#x95F4;</li>
</ul>
<p>HttpClient &#x83B7;&#x53D6;&#x8FDE;&#x63A5;&#x65F6;&#x662F;&#x4ECE;&#x8FDE;&#x63A5;&#x6C60;&#x4E2D;&#x83B7;&#x5F97;&#x7684;&#xFF0C;&#x5982;&#x679C;&#x8FDE;&#x63A5;&#x6C60;&#x4E2D;&#x7684;&#x8FDE;&#x63A5;&#x5DF2;&#x6EE1;&#x5E76;&#x4E14;&#x90FD;&#x5904;&#x5728;&#x8FDE;&#x63A5;&#x4E2D;&#xFF0C;&#x5982;&#x679C;&#x7B49;&#x5F85;&#x67D0;&#x6BB5;&#x65F6;&#x95F4;&#x83B7;&#x53D6;
&#x4E0D;&#x5230;&#x8FDE;&#x63A5;&#xFF0C;&#x5219;&#x5C31;&#x4F1A;&#x51FA;&#x73B0;&#x8FDE;&#x63A5;&#x8D85;&#x65F6;&#x7684;&#x60C5;&#x51B5;&#x3002;</p>
<p>&#x5BA2;&#x6237;&#x7AEF;&#x4EE3;&#x7801;&#x5982;&#x4E0B;&#xFF1A;</p>
<pre><code class="lang-java">    <span class="hljs-meta">@Test</span>
    <span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title">testConnectionRequestTimeout</span><span class="hljs-params">()</span> <span class="hljs-keyword">throws</span> InterruptedException</span>{
        PoolingHttpClientConnectionManager manager = <span class="hljs-keyword">new</span> PoolingHttpClientConnectionManager();
        <span class="hljs-comment">// &#x4E3A;&#x4E86;&#x6F14;&#x793A;&#x6548;&#x679C;&#xFF0C;&#x8BBE;&#x7F6E;&#x8FDE;&#x63A5;&#x6C60;&#x7684;&#x6700;&#x5927;&#x8FDE;&#x63A5;&#x6570;&#x662F; 3 &#x6216;&#x8005;&#x66F4;&#x5C11; (&#x9ED8;&#x8BA4;&#x662F;20)</span>
        manager.setMaxTotal(<span class="hljs-number">3</span>);
        CloseableHttpClient client = HttpClients.custom().setConnectionManager(manager).build();
        <span class="hljs-comment">// &#x4F7F;&#x7528;&#x7EBF;&#x7A0B;&#x5C06;&#x8FDE;&#x63A5;&#x8017;&#x5C3D;</span>
        <span class="hljs-keyword">for</span> (<span class="hljs-keyword">int</span> i=<span class="hljs-number">0</span>;i&lt;<span class="hljs-number">10</span>;i++){
            HttpGet httpGet = <span class="hljs-keyword">new</span> HttpGet(URL + <span class="hljs-string">&quot;http/testSocketTimeout&quot;</span>);
            HttpThread thread = <span class="hljs-keyword">new</span> HttpThread(client, httpGet);
            thread.start();
        }
        HttpGet httpGet = <span class="hljs-keyword">new</span> HttpGet(URL + <span class="hljs-string">&quot;http/testSocketTimeout&quot;</span>);
        <span class="hljs-comment">// &#x8BBE;&#x7F6E;&#x8D85;&#x65F6;&#x65F6;&#x95F4;&#x662F; 1000 &#x6BEB;&#x79D2;</span>
        RequestConfig requestConfig = RequestConfig.custom().setConnectionRequestTimeout(<span class="hljs-number">1000</span>).build();
        httpGet.setConfig(requestConfig);
        <span class="hljs-keyword">try</span> {
            CloseableHttpResponse response = client.execute(httpGet);
            System.out.println(EntityUtils.toString(response.getEntity()));
        } <span class="hljs-keyword">catch</span> (Exception e){
            e.printStackTrace();
        }
    }
</code></pre>
<p>&#x8BBE;&#x7F6E;&#x8FDE;&#x63A5;&#x6C60;&#x6700;&#x5927;&#x8FDE;&#x63A5;&#x6570;&#x662F; 3&#xFF0C;&#x7136;&#x540E;&#x542F;&#x7528;&#x591A;&#x4E2A;&#x7EBF;&#x7A0B;&#xFF0C;&#x5C06;&#x6B64;&#x8FDE;&#x63A5;&#x8FDB;&#x884C;&#x8017;&#x5C3D;&#xFF0C;&#x5F53;&#x518D;&#x6B21;&#x53D1;&#x8D77;&#x8C03;&#x7528;&#x65F6;&#xFF0C; &#x4ECE;&#x8FDE;&#x63A5;&#x6C60;&#x4E2D;&#x83B7;&#x53D6;&#x4E0D;&#x5230;&#x8FDE;&#x63A5;&#xFF0C;&#x6B64;&#x65F6;&#x629B;&#x51FA;&#x5F02;&#x5E38;&#xFF1A;</p>
<pre><code class="lang-java">org.apache.http.conn.ConnectionPoolTimeoutException: Timeout waiting <span class="hljs-keyword">for</span> connection from pool
</code></pre>
<p>&#x9644; HttpThread &#x4EE3;&#x7801;</p>
<pre><code class="lang-java"><span class="hljs-keyword">public</span> <span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">HttpThread</span> <span class="hljs-keyword">extends</span> <span class="hljs-title">Thread</span></span>{

    <span class="hljs-keyword">private</span> CloseableHttpClient closeableHttpClient;
    <span class="hljs-keyword">private</span> HttpGet httpGet;

    <span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-title">HttpThread</span><span class="hljs-params">(CloseableHttpClient closeableHttpClient, HttpGet httpGet)</span></span>{
        <span class="hljs-keyword">this</span>.closeableHttpClient = closeableHttpClient;
        <span class="hljs-keyword">this</span>.httpGet = httpGet;
    }


    <span class="hljs-meta">@Override</span>
    <span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title">run</span><span class="hljs-params">()</span> </span>{
        <span class="hljs-keyword">try</span> {
            CloseableHttpResponse response = closeableHttpClient.execute(httpGet);
            System.out.println(EntityUtils.toString(response.getEntity()));
            EntityUtils.consume(response.getEntity());
        } <span class="hljs-keyword">catch</span> (IOException e) {
            e.printStackTrace();
        }
    }
}
</code></pre>

                                
                                </section>
                            
    </div>
    <div class="search-results">
        <div class="has-results">
            
            <h1 class="search-results-title"><span class='search-results-count'></span> results matching "<span class='search-query'></span>"</h1>
            <ul class="search-results-list"></ul>
            
        </div>
        <div class="no-results">
            
            <h1 class="search-results-title">No results matching "<span class='search-query'></span>"</h1>
            
        </div>
    </div>
</div>

                        </div>
                    </div>
                
            </div>

            
                
                <a href="../" class="navigation navigation-prev " aria-label="Previous page: HttpClient">
                    <i class="fa fa-angle-left"></i>
                </a>
                
                
                <a href="httpclient-pool.html" class="navigation navigation-next " aria-label="Next page: HttpClient 连接池">
                    <i class="fa fa-angle-right"></i>
                </a>
                
            
        
    </div>

    <script>
        var gitbook = gitbook || [];
        gitbook.push(function() {
            gitbook.page.hasChanged({"page":{"title":"HttpClient几个超时","level":"1.7.1","depth":2,"next":{"title":"HttpClient 连接池","level":"1.7.2","depth":2,"path":"httpclient/httpclient-pool.md","ref":"httpclient/httpclient-pool.md","articles":[]},"previous":{"title":"HttpClient","level":"1.7","depth":1,"path":"README.md","ref":"README.md","articles":[{"title":"HttpClient几个超时","level":"1.7.1","depth":2,"path":"httpclient/httpclient-timeout.md","ref":"httpclient/httpclient-timeout.md","articles":[]},{"title":"HttpClient 连接池","level":"1.7.2","depth":2,"path":"httpclient/httpclient-pool.md","ref":"httpclient/httpclient-pool.md","articles":[]}]},"dir":"ltr"},"config":{"gitbook":"*","theme":"default","variables":{},"plugins":["toggle-chapters"],"pluginsConfig":{"toggle-chapters":{},"highlight":{},"search":{},"lunr":{"maxIndexSize":1000000,"ignoreSpecialCharacters":false},"sharing":{"facebook":true,"twitter":true,"google":false,"weibo":false,"instapaper":false,"vk":false,"all":["facebook","google","twitter","weibo","instapaper"]},"fontsettings":{"theme":"white","family":"sans","size":2},"theme-default":{"styles":{"website":"styles/website.css","pdf":"styles/pdf.css","epub":"styles/epub.css","mobi":"styles/mobi.css","ebook":"styles/ebook.css","print":"styles/print.css"},"showLevel":false}},"structure":{"langs":"LANGS.md","readme":"README.md","glossary":"GLOSSARY.md","summary":"SUMMARY.md"},"pdf":{"pageNumbers":true,"fontSize":12,"fontFamily":"Arial","paperSize":"a4","chapterMark":"pagebreak","pageBreaksBefore":"/","margin":{"right":62,"left":62,"top":56,"bottom":56}},"styles":{"website":"styles/website.css","pdf":"styles/pdf.css","epub":"styles/epub.css","mobi":"styles/mobi.css","ebook":"styles/ebook.css","print":"styles/print.css"}},"file":{"path":"httpclient/httpclient-timeout.md","mtime":"2019-09-08T13:54:11.874Z","type":"markdown"},"gitbook":{"version":"3.2.3","time":"2020-12-17T05:25:43.709Z"},"basePath":"..","book":{"language":""}});
        });
    </script>
</div>

        
    <script src="../gitbook/gitbook.js"></script>
    <script src="../gitbook/theme.js"></script>
    
        
        <script src="../gitbook/gitbook-plugin-toggle-chapters/toggle.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-search/search-engine.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-search/search.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-lunr/lunr.min.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-lunr/search-lunr.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-sharing/buttons.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-fontsettings/fontsettings.js"></script>
        
    

    </body>
</html>

